<template>
  <div class="not-found-container">
    <div class="not-found-content">
      <div class="error-code">404</div>
      <h1>页面未找到</h1>
      <p>抱歉，您访问的页面不存在或已被移除。</p>
      <div class="actions">
        <button class="btn btn-primary" @click="goHome">返回首页</button>
        <button class="btn btn-outline" @click="goBack">返回上一页</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

const router = useRouter()

const goHome = () => {
  // 检查用户是否已登录
  const token = localStorage.getItem('token')
  const userRole = localStorage.getItem('role')

  if (token) {
    // 已登录用户，根据角色导航到相应的首页
    if (userRole === 'admin') {
      router.push('/admin')
    } else {
      router.push('/dashboard')
    }
  } else {
    // 未登录用户，导航到登录页
    router.push('/login')
  }
}

const goBack = () => {
  // 如果有历史记录，则返回上一页
  if (window.history.length > 1) {
    router.go(-1)
  } else {
    // 如果没有历史记录，则执行与goHome相同的逻辑
    goHome()
  }
}
</script>

<style scoped>
.not-found-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.not-found-content {
  text-align: center;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  max-width: 500px;
}

.error-code {
  font-size: 120px;
  font-weight: 700;
  color: #4caf50;
  line-height: 1;
  margin-bottom: 20px;
}

h1 {
  font-size: 32px;
  margin-bottom: 16px;
  color: #333;
}

p {
  font-size: 18px;
  color: #666;
  margin-bottom: 32px;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.btn {
  padding: 12px 24px;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.btn-primary {
  background-color: #4caf50;
  color: white;
  border: none;
}

.btn-outline {
  background-color: transparent;
  border: 1px solid #4caf50;
  color: #4caf50;
}
</style>
